package com.kzd.sdk.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnClickListener;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.callback.KZD_OnSmallListener;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_LoginMain;
import com.kzd.sdk.entity.KZD_LoginSmall;
import com.kzd.sdk.entity.KZD_SmallGroup;
import com.kzd.sdk.ui.adapter.KZD_SmallAccountAdapter;
import com.kzd.sdk.ui.dialog.KZD_AccountSwitchDialog;
import com.kzd.sdk.ui.dialog.KZD_SmallAccountDialog;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

import static com.kzd.sdk.core.KZD_ApiConstance.GT_ACTIVITY_REQUEST_CODE;
import static com.kzd.sdk.core.KZD_ApiConstance.GT_ACTIVITY_RESULT_CODE;

public class KZD_EnterActivity extends OwnerActivity {

    public static void open(List<KZD_SmallGroup> data) {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_EnterActivity.class);
        if (data != null && data.size() != 0) {
            Bundle bundle = new Bundle();
            bundle.putString(KZD_ApiConstance.KEY_USER_SMALL, new Gson().toJson(data));
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, GT_ACTIVITY_REQUEST_CODE);
    }

    public static void open() {
        open(null);
    }

    private TextView tvEnterUsername;

    private KZD_SmallAccountAdapter mSmallAccountAdapter;
    private KZD_SmallAccountDialog mSmallAccountDialog;
    private KZD_AccountSwitchDialog mAccountSwitchDialog;

    //小号列表回调
    private final KZD_HttpCallback<List<KZD_SmallGroup>> httpListenerSmallList = new KZD_HttpCallback<List<KZD_SmallGroup>>() {
        @Override
        public void onSuccess(List<KZD_SmallGroup> data) {
            mSmallAccountAdapter.setNewInstance(data);
        }

        @Override
        public void onError(String message) {
            toast(message);
        }
    };

    //小号添加回调
    private final KZD_HttpCallback<String> httpListenerAdd = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSmallAccountDialog.cancel();
            KZD_ApiManager.httpSmallAccountList(httpListenerSmallList);
        }

        @Override
        public void onError(String message) {
            toast(message);
        }
    };

    //小号登录回调
    private final KZD_HttpCallback<KZD_LoginSmall> httpListenerLogin = new KZD_HttpCallback<KZD_LoginSmall>() {
        @Override
        public void onSuccess(KZD_LoginSmall data) {
            setResult(GT_ACTIVITY_RESULT_CODE);
            finish();
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_enter";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {
        tvEnterUsername = findViewById(AppUtils.getId((this), "tvEnterUsername"));
        TextView tvEnterSwitch = findViewById(AppUtils.getId((this), "tvEnterSwitch"));
        ImageView ivEnterService = findViewById(AppUtils.getId((this), "ivEnterService"));
        LinearLayout llEnterUserSmallAdd = findViewById(AppUtils.getId((this), "llEnterUserSmallAdd"));
        RelativeLayout rlEnterUserOpenGTGame = findViewById(AppUtils.getId((this), "rlEnterUserOpenGTGame"));
        RecyclerView rvEnterUserSmallBody = findViewById(AppUtils.getId((this), "rvEnterUserSmallBody"));

        //实例化小号列表适配器
        mSmallAccountAdapter = new KZD_SmallAccountAdapter((this), (1));
        //实例化小号创建对话框
        mSmallAccountDialog = new KZD_SmallAccountDialog((this));
        //实例化切换账号对话框
        mAccountSwitchDialog = new KZD_AccountSwitchDialog((this));

        rvEnterUserSmallBody.setLayoutManager(new LinearLayoutManager((this)));
        rvEnterUserSmallBody.setAdapter(mSmallAccountAdapter);

        //小号列表条目点击事件
        mSmallAccountAdapter.setOnItemClickListener(new KZD_OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                String smallUid = mSmallAccountAdapter.get(position).getUid();
                KZD_ApiManager.httpSmallAccountLogin(smallUid, httpListenerLogin);
            }
        });

        //添加小号确认点击事件
        mSmallAccountDialog.setOnClickListener(new KZD_OnSmallListener() {
            @Override
            public void onName(String name) {
                KZD_ApiManager.httpSmallAccountAdd(name, httpListenerAdd);
            }
        });

        //切换账号确认点击事件
        mAccountSwitchDialog.setOnClickListener(new KZD_OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_SdkController.getOnHideListener().onAccountSwitch();

                KZD_SdkController.getOnLoginListener().onLoginSwitch(KZD_ApiConstance.LOGIN_MAIN);
                KZD_AccessActivity.open();
                finish();
            }
        });

        //添加小号点击事件
        llEnterUserSmallAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSmallAccountDialog.show();
            }
        });

        //打开GT游戏盒子
        rlEnterUserOpenGTGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = KZD_EnterActivity.this;
                if (!AppUtils.isAppInstalled(context, KZD_ApiConstance.GT_GAME_PACKAGE)) {
                    AppUtils.launchBrowser(context, KZD_ApiConstance.GT_GAME_WEB);
                } else {
                    AppUtils.launchApp(context, KZD_ApiConstance.GT_GAME_PACKAGE);
                }
            }
        });

        //切换账号
        tvEnterSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAccountSwitchDialog.show();
            }
        });

        //客服中心
        ivEnterService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_UserServiceActivity.open((KZD_EnterActivity.this));
            }
        });
    }

    @Override
    public void initData() {

        KZD_LoginMain loginMain = KZD_SdkController.getLoginMain();
        if (loginMain != null) {
            tvEnterUsername.setText(loginMain.getUsername());
        }

        List<KZD_SmallGroup> list = new ArrayList<>();
        try {
            String json = getIntent().getExtras()
                    .getString(KZD_ApiConstance.KEY_USER_SMALL);
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                list.add(new Gson().fromJson(elem, KZD_SmallGroup.class));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list.size() == 0) {
            KZD_ApiManager.httpSmallAccountList(httpListenerSmallList);
        } else {
            mSmallAccountAdapter.setNewInstance(list);
        }
    }

}
